package com.flame.user.controller;

import javax.validation.constraints.NotBlank;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.user.controller.vo.MenuAddVo;
import com.flame.user.controller.vo.MenuBtnVo;
import com.flame.user.entity.Menu;
import com.flame.user.service.MenuService;
import com.flame.core.protocol.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * @author wuxintong
 * @since 2022/10/26
 */
@RestController
public class MenuController {

    @Autowired
    private MenuService menuService;

    @HasAuth("menu")
    @PostMapping("/menu/btn/list")
    public Result listMenuBtn(@RequestBody MenuBtnVo menuBtnVo) {
        return Result.success(menuService.listMenuBtn(menuBtnVo.getMenuCode()));
    }

    @PostMapping("/menu/list")
    public Result listMenu() {
        return Result.success(menuService.list());
    }

    @PostMapping("/menu")
    public Result menu(@RequestBody MenuBtnVo menuBtnVo) {
        return Result.success(menuService.getByMenuCode(menuBtnVo.getMenuCode()));
    }

    @HasAuth("menu:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/menu/add")
    public Result addMenu(@RequestBody @Validated MenuAddVo menuAddVo) {
        menuService.addMenu(menuAddVo);
        return Result.success();
    }

    @HasAuth("menu:edit")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/menu/update")
    public Result updateMenu(@RequestBody @Validated MenuAddVo menuAddVo) {
        menuService.updateMenu(menuAddVo);
        return Result.success();
    }

    @HasAuth("menu:delete")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/menu/delete")
    public Result deleteMenu(@RequestBody @Validated Menu menu) {
        menuService.deleteMenu(menu.getMenuCode());
        return Result.success();
    }
}
